package com.ukee.birthday.modules.controller;

import com.google.zxing.WriterException;
import com.ukee.birthday.common.utils.PageUtils;
import com.ukee.birthday.common.utils.QrCodeUtils;
import com.ukee.birthday.common.utils.R;
import com.ukee.birthday.common.validator.ValidatorUtils;
import com.ukee.birthday.modules.entity.MealEntity;
import com.ukee.birthday.modules.service.MealService;
import com.ukee.birthday.modules.service.StaffService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("meal")
public class MealController extends AbstractController {

    @Resource
    private MealService mealService;

    @Resource
    private StaffService staffService;

    /**
     * 订餐分页一览
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mealService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 取消订餐
     */
    @GetMapping("/cancel")
    public R cancel(@RequestParam Integer id){
        MealEntity mealEntity = mealService.getById(id);
        //1-已订餐  2-已取餐  3-未取餐  4-已取消
        if(mealEntity.getState() == 2){
            return R.error("当前已核销/取餐，不能取消！");
        }else if(mealEntity.getState() == 4){
            return R.error("当前订餐已被取消，不能再次取消！");
        }
        mealEntity.setState(4);
        mealService.updateById(mealEntity);
        return R.ok();
    }

    /**
     * 预订餐操作
     */
    @PostMapping("/save")
    public R save(@RequestBody MealEntity mealEntity){

        ValidatorUtils.validateEntity(mealEntity);

        mealEntity.setCreateTime(new Date());
        mealEntity.setState(1);
        mealService.save(mealEntity);

        // 生成二维码
        String codeUrl = null;
        try {
            String username = staffService.getById(mealEntity.getUid()).getUsername();
            String codeText = "id="+mealEntity.getId()+",uid="+mealEntity.getUid()+",username="+username+",state="+mealEntity.getState()+",createTime="+mealEntity.getCreateTime();
//            String codeText = mealEntity.toString();
            codeUrl =  QrCodeUtils.generateQRCodeImage(codeText,600,300);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().put("mealID",mealEntity.getId());

//        return R.ok().put("codUrl",codeUrl);
    }

    /**
     * 核销
     * mealId:订餐id
     * vId:核销人id
     */
    @GetMapping("/verify")
    public R verify(@RequestParam Integer mealId,@RequestParam Integer vId){
        MealEntity mealEntity = mealService.getById(mealId);
        if(mealEntity.getState() == 2){
            return R.error("当前订餐已被核销，请勿重复核销！");
        }
        if(mealEntity.getState() == 4){
            return R.error("当前订餐已被取消，不能核销！");
        }
        mealEntity.setState(2);
        mealEntity.setVid(vId);
        mealEntity.setVname(staffService.getById(vId).getUsername());
        mealEntity.setTakeTime(new Date());
        mealService.updateById(mealEntity);
        return R.ok();
    }

    /**
     * 【本年】出餐/核销记录
     * vId:核销人id
     */
    @GetMapping("/verifyList")
    public R verifyList(@RequestParam Integer vId){
        List<MealEntity> list = mealService.getVerifyList(vId);
        return R.ok().put("list",list);
    }

    /**
     * 出餐/核销记录--分页
     * vId:核销人id
     */
    @GetMapping("/vList")
    public R queryVerifyPage(@RequestParam Map<String, Object> params){
        PageUtils page = mealService.queryVerifyPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 员工的所有订餐记录
     */
    @GetMapping("/order")
    public R list(@RequestParam Integer uid){
        List<MealEntity> list = mealService.getList(uid);
        return R.ok().put("order",list);
    }

    /**
     * 员工本年的订餐记录
     * uid:员工id
     */
    @GetMapping("/yearOrder")
    public R yearOrder(@RequestParam Integer uid){
        List<MealEntity> list = mealService.getYearList(uid);
        return R.ok().put("order",list);
    }

    /**
     * 对应日期的订餐(不包括"已取消订单")
     * date:YY-mm-dd
     */
    @GetMapping("/dayOrder")
    public R dayOrder(@RequestParam String date){
        List<MealEntity> list = mealService.getDayOrder(date);
        return R.ok().put("order",list);
    }

    /**
     * 当月所有订餐记录(不包括"已取消订单")
     */
    @GetMapping("/monthOrder")
    public R dayOrder(){
        List<MealEntity> list = mealService.getMonthOrder();
        return R.ok().put("order",list);
    }

}
